package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.assets.driver.model.dto.DriverEvaluationDetailDto;
import com.yungu.swift.assets.driver.model.dto.DriverEvaluationDto;
import com.yungu.swift.assets.driver.model.param.AdminDriverEvaluationPageParam;
import com.yungu.swift.assets.driver.model.vo.DriverEvaluationDetailVo;
import com.yungu.swift.assets.driver.model.vo.DriverEvaluationInfoVo;
import com.yungu.swift.assets.driver.service.DriverEvaluationDetailService;
import com.yungu.swift.assets.driver.service.DriverEvaluationService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.common.model.enums.CommonDictionaryEnum;
import com.yungu.swift.common.model.param.CommonDictionaryParam;
import com.yungu.swift.common.model.vo.EvaluationItemVo;
import com.yungu.swift.common.service.CommonDictionaryService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/v1/operation/driver/evaluation")
@Api(value = "【运营管理】", tags = "司机个人测评")
public class DriverEvaluationAdmin {

    @Reference
    private DriverEvaluationService driverEvaluationService;

    @Reference
    private DriverEvaluationDetailService driverEvaluationDetailService;

    @Reference
    private CommonDictionaryService commonDictionaryService;

    @RequestMapping("/list")
    public ResponseData<PageVo<DriverEvaluationInfoVo>> list(@RequestBody AdminDriverEvaluationPageParam adminDriverEvaluationPageParam) throws Exception {
        return driverEvaluationService.queryPageVo(adminDriverEvaluationPageParam);
    }

    @RequestMapping("/addItem")
    public ResponseData<Boolean> addItem(@RequestBody DriverEvaluationDetailDto driverEvaluationDetailDto, @CurrentUser AdminParam adminParam) {
        Date nowDate = new Date();
        driverEvaluationDetailDto.setUuid(StringUtils.buildUUID());
        driverEvaluationDetailDto.setCreateBy(adminParam.getUserUuid());
        driverEvaluationDetailDto.setCreateOn(nowDate);
        driverEvaluationDetailDto.setUpdateOn(nowDate);
        driverEvaluationDetailDto.setUpdateBy(adminParam.getUserUuid());
        driverEvaluationDetailDto.setOperationTime(nowDate);
        DriverEvaluationDto driverEvaluationDto = driverEvaluationService.list(MapUtils.build("uuid", driverEvaluationDetailDto.getEvaluationUuid())).getData().get(0);
        DriverEvaluationDto editDriverEvaluationDto = new DriverEvaluationDto();
        editDriverEvaluationDto.setUuid(driverEvaluationDto.getUuid());
        editDriverEvaluationDto.setDriverScore(driverEvaluationDto.getDriverScore() + Double.parseDouble(driverEvaluationDetailDto.getItemScore()));
        driverEvaluationService.edit(editDriverEvaluationDto);
        return driverEvaluationDetailService.add(driverEvaluationDetailDto);
    }

    @RequestMapping("/getItems")
    public ResponseData<List<EvaluationItemVo>> addItem() {
        CommonDictionaryParam commonDictionaryParam = new CommonDictionaryParam();
        commonDictionaryParam.setDictKey(CommonDictionaryEnum.DRIVER_EVALUATION_ITEM.getCode());
        return commonDictionaryService.queryDictionaryInfo(commonDictionaryParam);
    }

    @RequestMapping("/exportEvaluationInfo")
    public void exportEvaluationInfo(HttpServletResponse response, @RequestBody AdminDriverEvaluationPageParam adminDriverEvaluationPageParam) throws Exception {
        List<DriverEvaluationInfoVo> list = driverEvaluationService.queryEvaluationInfo(adminDriverEvaluationPageParam).getData();
        OutputStream os = null;
        try {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(9, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(10, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(11, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(12, ((25 + 1) * 7 + 5) / 7 * 256);

            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("日期");
            row.createCell(2).setCellValue("司机号码");
            row.createCell(3).setCellValue("司机姓名");
            row.createCell(4).setCellValue("服务路线");
            row.createCell(5).setCellValue("出勤天数");
            row.createCell(6).setCellValue("总趟数");
            row.createCell(7).setCellValue("订单总数");
            row.createCell(8).setCellValue("完成订单数");
            row.createCell(9).setCellValue("总人数");
            row.createCell(10).setCellValue("取消订单数");
            row.createCell(11).setCellValue("取消单数占比");
            row.createCell(12).setCellValue("司机评分");

            for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
                Row dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                DriverEvaluationInfoVo itemInfo = list.get(rowNum - 1);
                for (int cellNum = 1; cellNum <= 12; cellNum++) {
                    Cell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            //日期
                            cell.setCellValue(itemInfo.getCalculatedMonth());
                            break;
                        }
                        case 2: {
                            //司机账号
                            cell.setCellValue(itemInfo.getDriverMobile());
                            break;
                        }
                        case 3: {
                            //司机名称
                            cell.setCellValue(itemInfo.getDriverName());
                            break;
                        }
                        case 4: {
                            //服务路线
                            cell.setCellValue(itemInfo.getWayName());
                            break;
                        }
                        case 5: {
                            //出勤天数
                            cell.setCellValue(itemInfo.getAttendanceDay());
                            break;
                        }
                        case 6: {
                            //总趟数
                            cell.setCellValue(itemInfo.getTripTotal());
                            break;
                        }
                        case 7: {
                            //订单总数
                            cell.setCellValue(itemInfo.getOrderTotal());
                            break;
                        }
                        case 8: {
                            //完成订单数
                            cell.setCellValue(itemInfo.getCompleteOrderTotal());
                            break;
                        }
                        case 9: {
                            //总人数
                            cell.setCellValue(itemInfo.getPassengerTotal());
                            break;
                        }
                        case 10: {
                            //取消订单数
                            cell.setCellValue(itemInfo.getCancelOrderTotal());
                            break;
                        }
                        case 11: {
                            //取消单数占比
                            cell.setCellValue((itemInfo.getCancelOrderRatio()) + "%");
                            break;
                        }
                        case 12: {
                            //司机评分
                            cell.setCellValue(itemInfo.getDriverScore());
                            break;
                        }
                        default:
                            break;
                    }
                }
            }
            response.setHeader("Content-Disposition", "attachment; filename=DriverEvaluation.xlsx");
            response.setContentType("application/vnd.ms-excel");

            os = response.getOutputStream();
            workbook.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    @PostMapping("/exportItemDetail")
    public void exportItemDetail(HttpServletResponse response, @RequestBody AdminDriverEvaluationPageParam adminDriverEvaluationPageParam) throws Exception {
        List<DriverEvaluationDetailVo> list = driverEvaluationService.queryItemDetail(adminDriverEvaluationPageParam).getData();
        OutputStream os = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((25 + 1) * 7 + 5) / 7 * 256);

            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("司机号码");
            row.createCell(2).setCellValue("司机姓名");
            row.createCell(3).setCellValue("类型");
            row.createCell(4).setCellValue("变动值");
            row.createCell(5).setCellValue("统计月份");
            row.createCell(6).setCellValue("日期");

            String driverMobile = "";
            int lastRow = 1;
            for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
                Row dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                DriverEvaluationDetailVo itemInfo = list.get(rowNum - 1);
                if (rowNum == 1) {
                    driverMobile = itemInfo.getDriverMobile();
                }
                for (int cellNum = 1; cellNum <= 6; cellNum++) {
                    Cell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            //司机号码
                            cell.setCellValue(itemInfo.getDriverMobile());
                            break;
                        }
                        case 2: {
                            cell.setCellValue(itemInfo.getDriverName());
                            break;
                        }
                        case 3: {
                            //类型
                            cell.setCellValue(StringUtils.nullToString(itemInfo.getItemName()));
                            break;
                        }
                        case 4: {
                            //变动值
                            cell.setCellValue(StringUtils.nullToString(itemInfo.getItemScore()));
                            break;
                        }
                        case 5: {
                            //统计月份
                            cell.setCellValue(itemInfo.getCalculatedMonth());
                            break;
                        }
                        case 6: {
                            //日期
                            cell.setCellValue(itemInfo.getOperationTime() == null ? "" : dateFormat.format(itemInfo.getOperationTime()));
                            break;
                        }
                        default:
                            break;
                    }
                }
                //合并司机号码与司机姓名
                if ((!driverMobile.equals(itemInfo.getDriverMobile()) && lastRow != rowNum) || rowNum == list.size()) {
                    int mergeRow = rowNum - 1;
                    //最后一条的时候 需要合并到当前行
                    if (rowNum == list.size()) {
                        mergeRow = rowNum;
                    }
                    // 起始行号，终止行号， 起始列号，终止列号
                    CellRangeAddress region1 = new CellRangeAddress(lastRow, mergeRow, 1, 1);
                    CellRangeAddress region2 = new CellRangeAddress(lastRow, mergeRow, 2, 2);
                    sheet.addMergedRegion(region1);
                    sheet.addMergedRegion(region2);
                    lastRow = rowNum;
                    driverMobile = itemInfo.getDriverMobile();
                }
            }

            response.setHeader("Content-Disposition", "attachment; filename=Items.xlsx");
            response.setContentType("application/vnd.ms-excel");

            os = response.getOutputStream();
            workbook.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }


}
